package com.example.cameraapp;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int REQUEST_THUMBNAIL = 1;

    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn_getPic = findViewById(R.id.getPicture);

        imageView = findViewById(R.id.imageView);

        btn_getPic.setOnClickListener(this);

        String sdPath = Environment.getExternalStorageDirectory().getPath();
        String picPath = sdPath + "/" + "temp.png";
        Log.e("sdPath1", picPath);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view){
        if (view.getId() == R.id.getPicture) {
            Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent1, REQUEST_THUMBNAIL);
        }
    }

    /**
     * 返回应用时回调方法
     * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK){
            if(requestCode == REQUEST_THUMBNAIL){
                Bundle bundle = data.getExtras();
                Bitmap bitMap = (Bitmap) bundle.get("data");
                imageView.setImageBitmap(bitMap);
            }
        }
    }
}